import 'package:flutter/services.dart';
import 'package:sanzijing/global.dart';
import 'package:flutter/material.dart';
import 'package:sanzijing/main_manager.dart';
import 'package:sanzijing/page/detail.dart';
import 'package:sanzijing/page/directory.dart';
import 'package:provider/provider.dart';

class IndexPage extends StatefulWidget {
  IndexPage({Key key}) : super(key: key);
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  @override
  void initState() {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    MainManager _mainManager = Provider.of<MainManager>(context);
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      decoration: BoxDecoration(
        color: Global.backgroundColor.withOpacity(0.3),
        image: DecorationImage(
          image: AssetImage("assets/images/bg.png"),
          repeat: ImageRepeat.repeat,
          fit: BoxFit.fill,
        ),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            child: Image.asset('assets/images/szj.png'),
          ),
          _mainManager.history == null
              ? Container()
              : Container(
                  margin: EdgeInsets.all(20),
                  padding: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    border: Border.all(
                      width: 1.5,
                      color: Global.primaryColor.withOpacity(0.5),
                    ),
                    borderRadius: BorderRadius.circular(5),
                    color: Global.backgroundColor.withOpacity(0.5),
                  ),
                  child: InkWell(
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => DetailPage(
                              id: _mainManager.history['id'].toString()),
                        ),
                      );
                    },
                    child: Row(
                      children: [
                        Text(
                          '上次学到：',
                          style: TextStyle(
                            color: Global.fontSecondColor,
                            fontFamily: 'fangzhengkai',
                            fontWeight: FontWeight.w500,
                            fontSize: 18,
                          ),
                        ),
                        Expanded(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                _mainManager.history['index'],
                                style: TextStyle(
                                  color: Global.fontSecondColor,
                                  fontFamily: 'fangzhengkai',
                                  fontWeight: FontWeight.w600,
                                  fontSize: 15,
                                ),
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                              ),
                              Text(
                                _mainManager.history['title'],
                                style: TextStyle(
                                  color: Global.fontColor,
                                  fontFamily: 'fangzhengkai',
                                  fontWeight: FontWeight.w600,
                                  fontSize: 13,
                                ),
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                              )
                            ],
                          ),
                        ),
                        SizedBox(width: 20),
                        Icon(
                          Icons.arrow_forward,
                          color: Global.fontColor,
                          size: 22,
                        )
                      ],
                    ),
                  ),
                ),
          InkWell(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => DirectoryPage(),
                ),
              );
            },
            child: Container(
              margin: EdgeInsets.only(top: 30),
              padding: EdgeInsets.fromLTRB(50, 13, 50, 13),
              decoration: BoxDecoration(
                // border: Border.all(
                //   width: 1.5,
                //   color: Global.primaryColor,
                // ),
                borderRadius: BorderRadius.circular(35),
                color: Global.primaryColor,
                gradient: LinearGradient(
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                  colors: [
                    // Color(0xFF2C42BE),
                    Color(0xFF7987DD),
                    // Color(0xFFB0B7E6),
                    // Color(0xFF6270C7),
                    Color(0xFF1B2F9F),
                  ],
                ),
              ),
              child: Text(
                '开始阅读',
                style: TextStyle(
                  color: Global.backgroundColor.withOpacity(0.8),
                  fontFamily: 'fangzhengkai',
                  fontWeight: FontWeight.w700,
                  fontSize: 20,
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
